<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,0)">添加权限</el-button>
        </div>
        <el-table row-key="id" :data="list" border >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" type="text" @click="hAdd(2,row.id)">添加</el-button>
              <el-button type="text" @click="hisEdit(row)">编辑</el-button>
              <el-button type="text" @click="Dele(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹层 -->
      <el-dialog :visible.sync="showDialog" title="弹层标题" @close="ChongZhi">
        <!-- 表单内容 -->
        <el-form label-width="100px" ref="refformData">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="queding">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
 import { getPermissionList,delPermission,addPermission,updatePermission } from '@/api/permissions'
 import {translation} from '@/utils/index'

export default {
  data() {
    return {
      isEdit:false,
      list:[],
      showDialog:false,
      formData:{
        name:'',
        code:'',
        description:'',
        enVisible:0,
        pid:'',
        type:''
      }
    }
  },
  created(){
  this.loadPermissionList()
  },
  methods:{
    // 发送请求,编辑数据
    async doisEdit(){
      try {
        const res = await updatePermission(this.formData)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermissionList()
        // 保存数据 
      } catch (err) {
        console.log('updatePermission', err)
      }
    },
    // 编辑按钮
    hisEdit(data){
    this.isEdit = true
    this.showDialog = true
    this.formData = {...data}
    },
    // 重置表单
    ChongZhi(){
    this.$refs.refformData.resetFields()
    this.formData ={
        name:'',
        code:'',
        description:'',
        enVisible:0,
        pid:'',
        type:''
      }
    },
    // 确定按钮
    queding(){
    this.isEdit ? this.doisEdit() : this.doAdd()
    },
    // 发送请求,添加数据
    async doAdd(){
      try {
        const res = await addPermission(this.formData)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermissionList()
        // 保存数据 
      } catch (err) {
        console.log('addPermission', err)
      }
    },
    // 添加按钮
    hAdd(type,pid){
     this.isEdit = false
     this.formData.type = type
     this.formData.pid = pid
     this.showDialog = true
    },
    // 发送请求,删除数据
  async Dele(id){
    try {
      const res = await delPermission(id)
      this.$message.success(res.message)
      this.loadPermissionList()
      // 保存数据 
    } catch (err) {
      console.log('delPermission', err)
    }
  },
    // 发送请求,获取数据
  async loadPermissionList(){
    try {
      const res = await getPermissionList()
      // console.log(res);
      this.list = translation(res.data) 
      // 保存数据 
    } catch (err) {
      console.log('getPermissionList', err)
    }
  },
  }
  }
</script>
